package com.xyyl.patient.controller;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.xyyl.patient.common.utils.CalculateAgeUtils;
import com.xyyl.patient.core.controller.BaseController;
import com.xyyl.patient.core.util.DateUtils;
import com.xyyl.patient.core.util.ResponseBodyResult;
import com.xyyl.patient.entity.archives.Archives;
import com.xyyl.patient.entity.archives.Visit;
import com.xyyl.patient.service.archives.ArchivesService;

/**
 * @ClassName: ArchivesController
 * @Description:档案信息管理 1.我的档案列表 2.添加档案 3.删除档案 4.查询档案列表
 * @author fangsheng
 * @date 2017年8月19日
 */
@RestController
@RequestMapping("/archives")
public class ArchivesController extends BaseController {

	private Logger logger = LogManager.getLogger(ArchivesController.class);

	@Autowired
	private ArchivesService archivesService;

	/**
	 * @Title: queryElectronicArchivesList
	 * @Description: 用户档案查询
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/queryElectronicArchivesList", method = RequestMethod.POST)
	public JSONObject queryElectronicArchivesList(@RequestParam Integer patientid) {

		logger.info("access method {}", "queryArchivesList");

		logger.info("patientid{}  ", patientid);
		List<Archives> list = archivesService.selectElectronicArchivesList(patientid);
		try {
			return ResponseBodyResult.success(list);

		} catch (Exception e) {
			return ResponseBodyResult.error("档案查询失败!", e);

		}
	}

	/**
	 * @Title: insertElectronicArchives
	 * @Description: 用户新增档案
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/insertElectronicArchives", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertElectronicArchives(Archives archives) {

		logger.info("access method {}", "insertElectronicArchives");
		logger.info("Archives {}", archives);
		if (archives.getName().length() == 0 || archives.getName() == "" || archives.getBirthday().length() == 0
				|| archives.getBirthday() == "" || archives.getPhoneNumber().length() == 0
				|| archives.getPhoneNumber() == "") {
			return ResponseBodyResult.fail("参数填写不全,请重新填写!");
		}
		Integer age = CalculateAgeUtils.getAge(archives.getBirthday());
		archives.setAge(age);
		archives.setCreatetime(DateUtils.currentDatetime());
		try {
			return ResponseBodyResult.success("提交成功", archivesService.insertElectronicArchives(archives));

		} catch (Exception e) {
			return ResponseBodyResult.error("档案新增失败!", e);

		}
	}

	/**
	 * @Title: updateElectronicArchives
	 * @Description: 根据archives更新档案
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/updateElectronicArchives", method = RequestMethod.POST)
	public JSONObject updateElectronicArchives(Archives archives) {

		logger.info("access method {}", "updateArchives");

		logger.info("archives", archives);

		try {

			Archives electronicArchives = archivesService.selectOne(archives.getId());
			if (electronicArchives == null) {
				return ResponseBodyResult.fail("电子档案不存在!");
			}
			if (archives.getBirthday() != null) {
				Integer age = CalculateAgeUtils.getAge(archives.getBirthday());
				archives.setAge(age);
			}
			return ResponseBodyResult.success("保存成功", archivesService.updateElectronicArchives(archives));

		} catch (Exception e) {
			return ResponseBodyResult.error("更新档案失败!", e);

		}
	}

	/**
	 * @Title: deleteArchives
	 * @Description: 根据用户id删除档案
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/deleteArchives", method = RequestMethod.GET)
	public JSONObject deleteArchives(@RequestParam Integer id) {

		logger.info("access method {}", "deleteArchives");
		logger.info("id {}", id);

		try {
			List<Archives> archives = archivesService.checkVisit(id);
			if (archives.size() > 0) {
				return ResponseBodyResult.success("该档案下存在精防记录");
			}
			return ResponseBodyResult.success(archivesService.deleteArchives(id));

		} catch (Exception e) {
			return ResponseBodyResult.error("删除档案失败!", e);

		}
	}

	/**
	 * @Title: checkArchives
	 * @Description: 根据用户id查询档案是否已存在
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/checkArchives", method = RequestMethod.POST)
	public JSONObject checkArchives(@RequestParam Integer patientid) {

		logger.info("access method {}", "checkArchives");
		logger.info("patientid {}", patientid);

		try {
			return ResponseBodyResult.success(archivesService.checkArchives(patientid));

		} catch (Exception e) {
			return ResponseBodyResult.error("校验档案失败!", e);

		}
	}

	/**
	 * @Title: get
	 * @Description: 根据档案id查询档案详情
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public JSONObject queryById(@RequestParam Integer archivesId) {

		logger.info("access method {}", "checkArchives");
		logger.info("archivesId {}", archivesId);

		try {
			return ResponseBodyResult.success(archivesService.selectOne(archivesId));

		} catch (Exception e) {
			return ResponseBodyResult.error("校验档案失败!", e);

		}
	}

	/**
	 * @Title: listTern
	 * @Description: 随访，信息，扩展三合一
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/listTern", method = RequestMethod.POST)
	public JSONObject listTern(@RequestParam Integer archivesId) {

		logger.info("access method {}", "listTern");
		logger.info("archivesId{}  ", archivesId);
		try {
			List<Visit> list = archivesService.queryListTern(archivesId);
			return ResponseBodyResult.success(list);

		} catch (Exception e) {
			return ResponseBodyResult.error("查询失败!", e);

		}
	}

}
